package com.alcatellucent.ldap.service;

import java.util.ArrayList;

import com.alcatellucent.vo.RoomVO;
import com.alcatellucent.vo.UserVO;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPSearchException;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;

public class LdapRoomService {

	LDAPConnection connection;

	public ArrayList<RoomVO> getRoomList(LDAPConnection connection) {

		System.out.println("Connection not null");
		ArrayList<RoomVO> resultlist = null;
		SearchResult searchResults;
		try {
			searchResults = connection.search("o=directoryRoot",
					SearchScope.SUB, "(&(|(cn=*))(objectclass=room))");
			if (searchResults != null) {
				System.out.println("search result not null");
				resultlist = new ArrayList<RoomVO>();
				long count = searchResults.getEntryCount();
				if (searchResults.getEntryCount() > 0) {
					System.out.println("Entry count > 0");
					RoomVO roomVO;
					for (int i = 0; i < count; i++) {
						System.out.println("Count value =" + i);
						SearchResultEntry entry = searchResults
								.getSearchEntries().get(i);
						if (entry != null) {
							System.out.println("Entry not null for " + i);
							roomVO = new RoomVO();
							String room = entry.getAttributeValue("cn");
							roomVO.setName(room);
							System.out.println(" "
									+ entry.getAttributeValue("cn"));
							roomVO.setDescription(entry
									.getAttributeValue("description"));
							System.out.println(" "
									+ entry.getAttributeValue("description"));
							if(room!=null) {
								ArrayList<UserVO> roomUsers = getRoomUsers(
										connection, "person", "inetorgperson",
										room);
								roomVO.setUserList(roomUsers);
							}
							resultlist.add(roomVO);
						}
					}
				}
			}
		} catch (LDAPSearchException e) {
			e.printStackTrace();
		}

		return resultlist;
	}

	public ArrayList<UserVO> getRoomUsers(LDAPConnection connection,
			String objectClass1, String objectClass2, String room) {

		System.out.println("Connection not null");
		SearchResult searchResults;
		ArrayList<UserVO> resultlist = null;
		try {
			System.out.println("Before search");
			searchResults = connection.search("o=Alcatel,o=directoryRoot",
					SearchScope.SUB, "(&" + "(|" + "(sn=*)" + ")" + "(&"
							+ "(objectclass=" + objectClass1 + ")"
							+ "(objectclass=" + objectClass2 + ")" + "(cn="
							+ room + ")" + ")" + ")");

			if (searchResults != null) {
				System.out.println("search result not null");
				resultlist = new ArrayList<UserVO>();
				long count = searchResults.getEntryCount();
				if (searchResults.getEntryCount() > 0) {
					System.out.println("Entry count > 0");
					UserVO userVO;
					for (int i = 0; i < count; i++) {
						System.out.println("Count value =" + i);
						SearchResultEntry entry = searchResults
								.getSearchEntries().get(i);
						if (entry != null) {
							System.out.println("Entry not null for " + i);
							userVO = new UserVO();
							userVO.setLastname(entry.getAttributeValue("sn"));
							System.out.println(" "
									+ entry.getAttributeValue("sn"));
							userVO.setFirstname(entry.getAttributeValue("cn"));
							System.out.println(" "
									+ entry.getAttributeValue("cn"));
							userVO.setCompany(entry.getAttributeValue("o"));
							System.out.println(" "
									+ entry.getAttributeValue("o"));
							userVO.setCountry(entry.getAttributeValue("c"));
							System.out.println(" "
									+ entry.getAttributeValue("c"));
							userVO.setName(entry.getAttributeValue("uid"));
							System.out.println(" "
									+ entry.getAttributeValue("uid"));
							userVO.setPassword(entry
									.getAttributeValue("userpassword"));
							System.out.println(" "
									+ entry.getAttributeValue("userpassword"));
							userVO.setMail(entry.getAttributeValue("mail"));
							System.out.println(" "
									+ entry.getAttributeValue("mail"));
							//userVO.setPhoto(entry.getAttributeValue("photo"));
							System.out.println(" "
									+ entry.getAttributeValue("photo"));
							userVO.setExtension(entry
									.getAttributeValue("telephonenumber"));
							System.out
									.println(" "
											+ entry.getAttributeValue("telephonenumber"));

							resultlist.add(userVO);
						}
					}
				}
			}
		} catch (Exception e) {

		}
		return resultlist;
	}

}